package com.lsh.payment.core.strategy.bill;

import com.alibaba.fastjson.JSON;
import com.lsh.payment.api.model.basevo.ExceptionStatus;
import com.lsh.payment.core.constant.BusinessConstant;
import com.lsh.payment.core.exception.BusinessException;
import com.lsh.payment.core.strategy.config.AllinPayConfig;
import com.lsh.payment.core.util.DateUtil;
import com.lsh.payment.core.util.HttpClientUtils;
import com.lsh.payment.core.util.PathUtil;
import com.lsh.payment.core.util.pay.allinpay.HttpConnectionUtil;
import com.lsh.payment.core.util.pay.allinpay.SybPayService;
import com.lsh.payment.core.util.pay.allinpay.SybUtil;
import org.apache.commons.lang.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.io.File;
import java.text.MessageFormat;
import java.util.Map;
import java.util.TreeMap;

/**
 * Project Name: lsh-payment
 *
 * @author peter
 * @date 16/11/24
 * 北京链商电子商务有限公司
 * Package
 * desc:
 */
public class AllinBillStrategy implements BillStrategy {

    private static Logger logger = LoggerFactory.getLogger(AllinBillStrategy.class);

    /**
     * 下载对账单
     *
     * @param billDate 查询参数
     * @param billType
     * @return
     */
    @Override
    public boolean downloadBillByDate(String billDate, int billType) {
        String prefix = "【请求通联下载对账单】 ";
        if(StringUtils.isBlank(billDate)){
            billDate = DateUtil.getYesterdayStr();
        }
        String downLoadPath = BusinessConstant.BILL_DOWNLOAD_PATH_ALLIN + File.separator + DateUtil.getDateString();
        PathUtil.createPath(downLoadPath);

        TreeMap<String, String> params = new TreeMap<>();
        params.put("cusid", AllinPayConfig.CUS_ID);
        params.put("appid", AllinPayConfig.APP_ID);
        params.put("date", billDate);
        params.put("randomstr", SybUtil.getValidatecode(8));
        HttpConnectionUtil http = new HttpConnectionUtil(AllinPayConfig.PAY_BASE + AllinPayConfig.PAY_BILL);
        Map<String, String> map;
        String result;
        try {
            http.init();
            params.put("sign", SybUtil.sign(params, AllinPayConfig.KEY));

            logger.info("[{}] 下单参数 {}", prefix, JSON.toJSONString(params));

            byte[] bys = http.postParams(params, true);
            result = new String(bys, "UTF-8");

            map = SybPayService.handleResult(result);
            logger.info("[{}] result is {}",prefix,result);
            String billDownloadUrl = map.get("url");

            if (StringUtils.isBlank(billDownloadUrl)) {
                logger.info("billDownloadUrl is null or checkRSASign is false");
                return false;
            }

            String billZipName = MessageFormat.format(AllinPayConfig.ALLIN_BILL_ZIP_NAME, billDate);

            logger.info("【download bill start】");
            HttpClientUtils.doPostAliBill(billDownloadUrl, downLoadPath, billZipName);
            logger.info("【download bill end】 ");

        } catch (Exception e) {
            e.printStackTrace();
            throw new BusinessException(ExceptionStatus.E2004004.getCode(), ExceptionStatus.E2004004.getMessage());
        }

        return false;
    }
}
